package net.gyapp.markim.module.basic;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.FrameLayout;

import com.easyder.wrapper.base.view.WrapperSwipeActivity;
import com.easyder.wrapper.core.model.BaseVo;
import com.just.library.AgentWeb;
import com.just.library.AgentWebSettings;
import com.just.library.ChromeClientCallbackManager;
import com.just.library.WebDefaultSettingsManager;

import net.gyapp.markim.module.basic.base.CommonPresenter;

import me.winds.widget.usage.TitleView;

/**
 * Auther:  winds
 * Data:    2017/11/3
 * Version: 1.0
 * Desc:
 */


public class BaseWebActivity extends WrapperSwipeActivity<CommonPresenter> {

    AgentWeb mAgentWeb;

    public static Intent getIntent(Context context, String url) {
        return new Intent(context, BaseWebActivity.class).putExtra("url", url);
    }

    @Override
    public int getViewLayout() {
        return -1; //返回0不通过xml向根布局添加布局
    }

    @Override
    protected void initView(Bundle savedInstanceState, final TitleView titleView, Intent intent) {
        mAgentWeb = AgentWeb.with(this)
                .setAgentWebParent((ViewGroup) getContainerView(), new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))//
                .useDefaultIndicator()//
                .defaultProgressBarColor()
                .setAgentWebSettings(new WebDefaultSettingsManager() {

                    @Override
                    public AgentWebSettings toSetting(WebView webView) {
                        AgentWebSettings settings = super.toSetting(webView);
                        WebSettings webSettings = settings.getWebSettings();
                        //设置参数
                        return settings;
                    }
                })
                .setReceivedTitleCallback(new ChromeClientCallbackManager.ReceivedTitleCallback() {
                    @Override
                    public void onReceivedTitle(WebView view, String title) {
                        titleView.setCenterText(title);
                    }
                })
                .setWebChromeClient(new WebChromeClient() {
                    @Override
                    public void onReceivedTitle(WebView view, String title) {
                        super.onReceivedTitle(view, title);
                    }
                })
                .setSecutityType(AgentWeb.SecurityType.strict)
                .createAgentWeb()//
                .ready()
                .go(intent.getStringExtra("url"));
    }


    @Override
    public void showContentView(String url, BaseVo dataVo) {

    }

    @Override
    protected void loadData(Bundle savedInstanceState, Intent intent) {

    }

    @Override
    public void onResume() {
        mAgentWeb.getWebLifeCycle().onResume();
        super.onResume();
    }

    @Override
    public void onPause() {
        mAgentWeb.getWebLifeCycle().onPause();
        super.onPause();
    }


    @Override
    protected void onDestroy() {
        mAgentWeb.getWebLifeCycle().onDestroy();
        super.onDestroy();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (mAgentWeb.handleKeyEvent(keyCode, event)) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        mAgentWeb.uploadFileResult(requestCode, resultCode, data);
        super.onActivityResult(requestCode, resultCode, data);
    }

}
